/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.maleskoding.jmplurk.ui.components;

import com.maleskoding.jmplurk.JMPlurk;
import com.maleskoding.jmplurk.plurkapi.PlurkException;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;
import java.io.IOException;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

/**
 *
 * @author Petra Barus
 */
public class PlurkViewComponent extends Container {

    private String plurkID = "";

    public PlurkViewComponent() {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        init();
    }

    /**
     * Initialize components
     */
    public void init() {
        this.setFocusable(false);
        //this.setScrollable(false);
        this.setScrollableX(false);
        this.setScrollableY(true);
        //this.setHandlesInput(true);
        //this.setSmoothScrolling(false);
    }

    public void loadPlurk(String plurkId) throws PlurkException, IOException, JSONException {
        removeAll();
        JSONObject getplurk = JMPlurk.plurk.getPlurk(plurkId);
        renderPlurk(getplurk);
        JSONObject getresponses = JMPlurk.plurk.getResponses(plurkId);
        renderResponses(getresponses);
        //TODO: Mark as read
    }

    public void renderPlurk(JSONObject plurkjson) throws JSONException {
        JSONObject plurk = plurkjson.getJSONObject("plurk");
        JSONObject user = plurkjson.getJSONObject("user");
        String qualifier = ((!plurk.isNull("qualifier_translated")) ? plurk.getString("qualifier_translated") : plurk.getString("qualifier"));

        Container plurkContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        plurkContainer.getStyle().setMargin(0, 0, 0, 0);
        plurkContainer.setFocusable(false);
        plurkContainer.setScrollable(false);
        plurkContainer.setScrollableY(false);
        plurkContainer.setScrollableX(false);
        plurkContainer.getStyle().setBgColor(0xFFFFFF);
        plurkContainer.getStyle().setFgColor(0x000000);
        plurkContainer.getStyle().setBgTransparency(0xFF);
        Label ownerLabel = new Label("Username");
        ownerLabel.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
        ownerLabel.getStyle().setBgColor(0xFFFFFF);
        ownerLabel.getStyle().setFgColor(0x000000);
        ownerLabel.getStyle().setMargin(0, 0, 0, 0);
        ownerLabel.getStyle().setPadding(0, 0, 0, 0);
        ownerLabel.getStyle().setBgTransparency(0);
        ownerLabel.setText((!user.isNull("display_name") ? user.getString("display_name") : user.getString("nick_name")));
        TextArea contentText = new TextArea();
        contentText.getStyle().setMargin(0, 0, 0, 0);
        contentText.getStyle().setPadding(0, 0, 0, 0);
        contentText.getStyle().setBgTransparency(0);
        contentText.getStyle().setFgColor(0x000000);
        contentText.getStyle().setBorder(Border.createEmpty());
        contentText.setEditable(false);
        contentText.setFocusable(false);
        contentText.setGrowByContent(true);
        contentText.setWidth(Display.getInstance().getDisplayWidth());

        contentText.setText(qualifier + " " + plurk.getString("content_raw"));
        Label dateLabel = new Label("Date");
        dateLabel.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        dateLabel.getStyle().setMargin(0, 0, 5, 5);
        dateLabel.getStyle().setPadding(0, 5, 0, 0);
        dateLabel.getStyle().setBgColor(0xFFFFFF);
        dateLabel.getStyle().setFgColor(0x000000);
        dateLabel.getStyle().setBgTransparency(0);
        dateLabel.setText(plurk.getString("posted"));
        plurkContainer.addComponent(ownerLabel);
        plurkContainer.addComponent(contentText);
        plurkContainer.addComponent(dateLabel);
        this.addComponent(plurkContainer);
    }

    public void renderResponses(JSONObject responses) throws JSONException {
        JSONObject friends;
        if (!responses.isNull("friends")) {
            friends = responses.getJSONObject("friends");
        } else {
            friends = responses.getJSONObject("friend");
        }

        JSONArray arresponses = responses.getJSONArray("responses");
        for (int i = 0; i < arresponses.length(); i++) {
            JSONObject response = arresponses.getJSONObject(i);
            JSONObject friend = friends.getJSONObject(response.getString("user_id"));
            String responseId = response.getString("id");
            String friendsName = "";
            if (!friend.isNull("display_name")) {
                friendsName = friend.getString("display_name");
            } else {
                friendsName = friend.getString("nick_name");
            }
            String qualifier = (!response.isNull("qualifier_translated")) ? response.getString("qualifier_translated") : response.getString("qualifier");
            String content = qualifier + " " + response.getString("content_raw");
            String date = response.getString("posted");

            //Creating Style
            Style selectedStyle = new Style();
            selectedStyle.setBgColor(0x000000);
            selectedStyle.setBgTransparency(100);
            selectedStyle.setBgColor(0x000000);
            selectedStyle.setMargin(0, 0, 0, 0);
            selectedStyle.setPadding(0, 0, 0, 0);
            //Creating Container
            Container plurkContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
            plurkContainer.getStyle().setBgTransparency(0);
            plurkContainer.getStyle().setMargin(3, 3, 1, 1);
            plurkContainer.setFocusable(true);
            plurkContainer.setSelectedStyle(selectedStyle);
            plurkContainer.setScrollable(true);
            plurkContainer.setScrollableY(true);
            plurkContainer.setScrollableX(false);
            //plurkContainer.putClientProperty("plurkID", plurkID);
            //plurkContainer.addFocusListener(this);
            //Creating user label
            Label userLabel = new Label(friendsName);
            userLabel.getStyle().setMargin(0, 0, 0, 0);
            userLabel.getStyle().setPadding(0, 0, 0, 0);
            userLabel.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
            userLabel.getStyle().setBgTransparency(0);
            //Creating date label
            Label dateLabel = new Label(date);
            dateLabel.getStyle().setMargin(0, 0, 0, 0);
            dateLabel.getStyle().setPadding(0, 0, 0, 0);
            dateLabel.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
            dateLabel.getStyle().setBgTransparency(0);
            //Creating content label
            TextArea contentText = new TextArea(content);
            contentText.getStyle().setMargin(0, 0, 0, 0);
            contentText.getStyle().setPadding(0, 0, 0, 0);
            contentText.getStyle().setBgTransparency(0);
            contentText.getStyle().setBorder(Border.createEmpty());
            contentText.setEditable(false);
            contentText.setFocusable(false);
            contentText.setGrowByContent(true);
            contentText.setWidth(Display.getInstance().getDisplayWidth());
            //Adding all components
            plurkContainer.addComponent(userLabel);
            plurkContainer.addComponent(contentText);
            plurkContainer.addComponent(dateLabel);

            //Adding the container
            //this.addComponent(plurkContainer);
            this.addComponent(plurkContainer);
        }
    }
}
